package cn.csc.module.pay.dal.mysql.jzbtask;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.mybatis.core.mapper.BaseMapperX;
import cn.csc.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.csc.module.pay.controller.app.jzbtask.vo.AppPayJzbTaskExportReqVO;
import cn.csc.module.pay.controller.app.jzbtask.vo.AppPayJzbTaskPageReqVO;
import cn.csc.module.pay.dal.dataobject.jzbtask.PayJzbTaskDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.math.BigInteger;
import java.util.List;

/**
 * 见证宝任务 Mapper
 *
 * @author liuchuang
 */
@Mapper
public interface PayJzbTaskMapper extends BaseMapperX<PayJzbTaskDO> {

    default PageResult<PayJzbTaskDO> selectPage(AppPayJzbTaskPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<PayJzbTaskDO>()
                .eqIfPresent(PayJzbTaskDO::getBusiType, reqVO.getBusiType())
                .eqIfPresent(PayJzbTaskDO::getOrderId, reqVO.getOrderId())
                .eqIfPresent(PayJzbTaskDO::getJzbOrderId, reqVO.getJzbOrderId())
                .eqIfPresent(PayJzbTaskDO::getTaskTime, reqVO.getTaskTime())
                .eqIfPresent(PayJzbTaskDO::getTaskStatus, reqVO.getTaskStatus())
                .eqIfPresent(PayJzbTaskDO::getTaskNum, reqVO.getTaskNum())
                .eq(PayJzbTaskDO::getDeleted, 0)
                .orderByDesc(PayJzbTaskDO::getId));
    }

    default List<PayJzbTaskDO> selectList(AppPayJzbTaskExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<PayJzbTaskDO>()
                .eqIfPresent(PayJzbTaskDO::getBusiType, reqVO.getBusiType())
                .eqIfPresent(PayJzbTaskDO::getOrderId, reqVO.getOrderId())
                .eqIfPresent(PayJzbTaskDO::getJzbOrderId, reqVO.getJzbOrderId())
                .betweenIfPresent(PayJzbTaskDO::getTaskTime, reqVO.getTaskTime())
                .eqIfPresent(PayJzbTaskDO::getTaskStatus, reqVO.getTaskStatus())
                .eqIfPresent(PayJzbTaskDO::getTaskNum, reqVO.getTaskNum())
                .betweenIfPresent(PayJzbTaskDO::getCreateTime, reqVO.getCreateTime())
                .eq(PayJzbTaskDO::getDeleted, 0)
                .orderByDesc(PayJzbTaskDO::getId));
    }

    /**
     * 外部订单一个支付订单只会有一条记录
     * @param jzbOrderId 见证宝订单ID
     * @return
     */
    default PayJzbTaskDO selectByJzbOrderId(BigInteger jzbOrderId){
        return selectOne(new LambdaQueryWrapperX<PayJzbTaskDO>()
                .eq(PayJzbTaskDO::getJzbOrderId, jzbOrderId)
        );
    }

    default PayJzbTaskDO selectByAppUserIdAndOrderItemId(BigInteger jzbOrderId, String appUserId, BigInteger orderItemId){
        return selectOne(new LambdaQueryWrapperX<PayJzbTaskDO>()
                .eq(PayJzbTaskDO::getJzbOrderId, jzbOrderId)
                .eq(PayJzbTaskDO::getAppUserId, appUserId)
                .eq(PayJzbTaskDO::getOrderItemId, orderItemId)
        );
    }

    /**
     * 内部订单一个支付订单会有多条记录
     * @param jzbOrderId 见证宝订单ID
     * @return
     */
    default List<PayJzbTaskDO> selectListByJzbOrderId(BigInteger jzbOrderId){
        return selectList(new LambdaQueryWrapperX<PayJzbTaskDO>()
                .eq(PayJzbTaskDO::getJzbOrderId, jzbOrderId)
        );
    }

    /**
     * 更新任务状态
     * @param id 订单ID
     * @param taskStatus 任务状态
     * @return
     */
    default Integer updateTaskStatus(BigInteger id, Integer taskStatus) {
        LambdaUpdateWrapper<PayJzbTaskDO> userUpdateWrapper = new LambdaUpdateWrapper<>();
        userUpdateWrapper.set(PayJzbTaskDO::getTaskStatus, taskStatus);
        userUpdateWrapper.eq(PayJzbTaskDO::getId, id);
        return update(null, userUpdateWrapper);

    }


}
